package com.hiultra.archive.device.print;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.fazecast.jSerialComm.SerialPort;
import com.gg.reader.api.dal.*;
import com.gg.reader.api.protocol.gx.*;
import com.hiultra.archive.config.MainConfig;
import com.hiultra.archive.controller.print.javagodex.demos.web.GodexPrinter;
import com.hiultra.archive.controller.print.javagodex.demos.web.NullAndEmptyUtil;
import com.hiultra.archive.device.door.TagInfo;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.service.*;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component("printManager")
public class PrintManager {
    private static int isConnect = 0;
    Log log = LogFactory.get();

    //创建打印机对象
    private static GodexPrinter godexPrinter = new GodexPrinter();

    public static int heartBeat() {
        //设置打印机连接属性 1连接成功 0连接失败（设置打印机连接 USB或串口）
        System.out.println("心跳包");
        return godexPrinter.API.openport("6");
    }

    public static int printLabel(String labelSize, ArcBox arcBox) throws RuntimeException, UnsupportedEncodingException {
        if (isConnect == 0) {
            isConnect = godexPrinter.API.openport("6");
        }
        heartBeat();
        if (NullAndEmptyUtil.isNullObject(labelSize)) {
            throw new RuntimeException("labelSize不能为空！");
        }
        //字体
        String FontName = "黑体";
        //档案盒号
        String boxNoStr = "盒号:" + arcBox.getBoxCode();
        //档案标签号
        String rfidStr = "标签号:" + arcBox.getRfid();
        //档案存放位置
        String storLocStr = "存放位置:" + arcBox.getStorLoc();

        //设置输出的内容的字节
        byte[] byteDataBoxNo = boxNoStr.getBytes("UTF-16LE");
        byte[] byteDataRfid = rfidStr.getBytes("UTF-16LE");
        byte[] byteDataStoLoc = storLocStr.getBytes("UTF-16LE");

        //请求前需设置
        godexPrinter.API.sendcommand("^L");
        //30毫米标签尺寸
        godexPrinter.API.setup(30, 12, 2, 0, 3, 0);
        //30毫米标签尺寸 文字
        godexPrinter.API.ecTextOutW(200, 70, 35, FontName, byteDataBoxNo, boxNoStr.length());
        //30毫米标签尺寸 文字
        godexPrinter.API.ecTextOutW(200, 150, 35, FontName, byteDataRfid, rfidStr.length());
        //30毫米标签尺寸 文字
        //godexPrinter.API.ecTextOutW(100,150 , 35, FontName, byteDataStoLoc, storLocStr.length());
        //30毫米标签尺寸  条码
//        godexPrinter.API.Bar("A3", 220, 60, 2, 4, 80, 0, 0, barcodeData);
        int code = godexPrinter.API.sendcommand("E");
        godexPrinter.API.closeport();
        return code;
    }

}
